﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using DBAccess.Object;
using System.Configuration;

public partial class UploadPicture : System.Web.UI.Page
{
    private string EncodeString(string value)
    {
        Byte[] originalBytes;
        Byte[] encodedBytes;
        MD5 md5;
        md5 = new MD5CryptoServiceProvider();
        originalBytes = ASCIIEncoding.Default.GetBytes(value);
        encodedBytes = md5.ComputeHash(originalBytes);
        return BitConverter.ToString(encodedBytes);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string host = ConfigurationManager.AppSettings["userHost"];
            string title = Request["txtTitle"];
            string lead = Request["txtLead"];
            int typeSubject = int.Parse(Request["hdChooseType"]);
            string linkVideo = Request["txtLinkVideo"];
            string fileUpload = "";
            Random r = new Random();
            string s = r.Next(100000).ToString() + DateTime.Now.ToString() + "" + DateTime.Now.Ticks.ToString();
            if (flUpload.FileName != "")
            {
                string img = s + flUpload.FileName;
                img = EncodeString(img) + ".jpg";
                string image = Server.MapPath("~/SubjectImages/" + img);
                flUpload.PostedFile.SaveAs(image);
                fileUpload = "/SubjectImages/" + img;
            }
            string description = "";
            if (typeSubject == 1)
            {
                string imagePath = host + fileUpload;
                description = "<p><img class=\"left\" src=\""+imagePath+"\" /></p>";
            }
            else if (typeSubject == 2)
            {
                
                description = "<p><iframe allowfullscreen=\"\" frameborder=\"0\" height=\"480\" src=\""+linkVideo+"\" width=\"640\"></iframe></p>";
            }
            Subject subject = new Subject();
            subject.Title = title;
            subject.Lead = lead;
            subject.ImagePath = fileUpload;
            subject.AuthorID = 0;
            subject.Content = description;
            subject.TypeSubject = typeSubject;
            subject.IconSubject = "";
            subject.LinkSubject = "";
            subject.Original = "";
            subject.MetaKey = "";
            subject.Priority = 1;
            subject.PublishDate = DateTime.Now;
            subject.CreateDate = DateTime.Now;
            subject.Status = 1;
            subject.TotalComment = 0;
            subject.TotalLike = 0;
            subject.TotalView = 0;
            subject.Visible = 1;
            subject.AdminID = 0;

            bool rs = DBAccess.Business.SubjectBusiness.InsertSubject(subject);
            if (rs)
            {

            }
            else
            {

            }
        }
        catch
        {

        }
    }
}